package handlers

import (
	"car-drive/cache"
	"car-drive/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	carNo := c.PostForm("carNo")
	carName := c.PostForm("carName")
	carPrice := c.PostForm("carPrice")
	if carNo == "" || carName == "" || carPrice == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	car := models.Car{
		CarNo:    carNo,
		CarName:  carName,
		CarPrice: carPrice,
	}
	err := models.DB.Create(&car).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func CarOrderAdd(c *gin.Context) {
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	orderNo := c.PostForm("orderNo")
	fromAdds := c.PostForm("fromAdds")
	toAdds := c.PostForm("toAdds")
	if cid == 0 || orderNo == "" || fromAdds == "" || toAdds == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	carOrder := models.CarOrder{
		OrderNo:  orderNo,
		FormAdds: fromAdds,
		ToAdds:   toAdds,
		Cid:      cid,
	}
	err := models.DB.Create(&carOrder).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func CarOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var list []models.CarOrderList
	redis_key := "list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("car_orders").
			Select("car_orders.id AS OrderId",
				"cars.car_no AS CarNo",
				"cars.car_name AS CarName",
				"cars.car_price AS CarPrice",
				"car_orders.order_no AS OrderNo",
				"car_orders.form_adds AS FormAdds",
				"car_orders.to_adds AS ToAdds",
			).
			Joins("LEFT JOIN cars ON cars.id = car_orders.cid").
			Offset(offset).
			Limit(size).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var count int64
	models.DB.Model(&models.CarOrder{}).Count(&count)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"count": count,
			"list":  list,
		},
	})
	return
}

func CarOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var list models.CarOrderList
	redis_key := "detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("car_orders").
			Select("car_orders.id AS OrderId",
				"cars.car_no AS CarNo",
				"cars.car_name AS CarName",
				"cars.car_price AS CarPrice",
				"car_orders.order_no AS OrderNo",
				"car_orders.form_adds AS FormAdds",
				"car_orders.to_adds AS ToAdds",
			).
			Joins("LEFT JOIN cars ON cars.id = car_orders.cid").
			Where("car_orders.id = ?", id).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
